/**
 * 
 */
package au.edu.swin.videoshop.publicapp.ui.sections;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.component.UIData;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import au.edu.swin.videoshop.bean.Classification;
import au.edu.swin.videoshop.bean.Title;
import au.edu.swin.videoshop.bean.TitleCategory;
import au.edu.swin.videoshop.bean.TitleSearchCriteria;
import au.edu.swin.videoshop.service.TitleService;

/**
 * @author julie
 *
 */
public class TitleSearchSection implements Serializable {
	private List mSearchResults;
	private List mCategories;
	private List mClassifications;
	private TitleSearchCriteria mSearchCriteria;
	
	@EJB
	private TitleService mTitleService;
	
	public TitleSearchSection(){
		mSearchCriteria = new TitleSearchCriteria();
	}

	/**
	 * @return the searchCriteria
	 */
	public TitleSearchCriteria getSearchCriteria() {
		return mSearchCriteria;
	}
	
	protected void sortWithComparator(Comparator comparator) {
		Collections.sort(mSearchResults, comparator);
	}

	public String sortByYear() {
		sortWithComparator(new Title.YearComparator());
		return null;
	}

	public String sortByTitle() {
		sortWithComparator(new Title.TitleNameComparator());
		return null;
	}
	
	public String sortByClassification() {
		sortWithComparator(new Title.ClassificationComparator());
		return null;
	}

	/**
	 * @return the searchResults
	 */
	public List getSearchResults() {
		return mSearchResults;
	}

	/**
	 * @return the categories
	 */
	public List getCategories() {
		if (mCategories == null) {
			mCategories = new ArrayList();
			mCategories.add(new SelectItem(null, "All categories"));
			List cats = mTitleService.findAllCategories();
			for (Iterator i = cats.iterator(); i.hasNext();) {
				TitleCategory cat = (TitleCategory)i.next();
				mCategories.add(new SelectItem(cat.getTitleCategoryId(), cat.getCategoryName()));
			}
		}
		return mCategories;
	}
	
	/**
	 * @return the classifications
	 */
	public List getClassifications() {
		if (mClassifications == null) {
			mClassifications = new ArrayList();
			mClassifications.add(new SelectItem(null, "All"));
			mClassifications.add(new SelectItem(Classification.G.getName(), Classification.G.getName()));
			mClassifications.add(new SelectItem(Classification.PG.getName(), Classification.PG.getName()));
			mClassifications.add(new SelectItem(Classification.M.getName(), Classification.M.getName()));
			mClassifications.add(new SelectItem(Classification.MA.getName(), Classification.MA.getName()));
			mClassifications.add(new SelectItem(Classification.R.getName(), Classification.R.getName()));
			mClassifications.add(new SelectItem(Classification.X.getName(), Classification.X.getName()));
			
		}
		return mClassifications;
	}

	public String search() {
		mSearchResults = mTitleService.findTitlesBySearchCriteria(mSearchCriteria);
		return "search";
	}
	
	/**
	 * Sets up the detail page by getting the bean backing the current row in
	 * the UIData table and passing it to the detail page.
	 * 
	 * @return the navigation outcome.
	 */   
	public String details() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UIViewRoot root = facesContext.getViewRoot();
		UIData table = (UIData)root.findComponent("searchResultForm").findComponent("searchResultTable");
		Title title = (Title) table.getRowData();
		
		TitleSection titleDetailsSection = (TitleSection)facesContext.getApplication().evaluateExpressionGet(facesContext, "#{titleSection}", TitleSection.class);
		
		titleDetailsSection.setSelectedTitle(title);
	
		return "details";
	}
}
